package database

import (
	"awesomeProject/internal/config"
	"context"
	"fmt"
	"time"

	"github.com/go-redis/redis"
)

func InitRedis(config *config.RedisConfig) *redis.Client {
	client := redis.NewClient(&redis.Options{
		Addr:         fmt.Sprintf("%s:%d", config.Host, config.Port),
		Password:     config.Password,
		DB:           config.DB,
		PoolSize:     10,
		MinIdleConns: 5,
		DialTimeout:  5 * time.Second,
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 3 * time.Second,
	})
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := client.WithContext(ctx).Ping().Err(); err != nil {
		panic("Failed to connect to Redis," + err.Error())
	}
	return client
}
